package com.ecotope.quartz.module.schedule;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ecotope.quartz.module.entity.AccessData;
import com.ecotope.quartz.module.entity.AtmosphericStation;
import com.ecotope.quartz.module.entity.TownAtmosphericStation;
import com.ecotope.quartz.module.opentsdb.ExpectResponse;
import com.ecotope.quartz.module.opentsdb.HttpClientImpl;
import com.ecotope.quartz.module.opentsdb.builder.MetricBuilder;
import com.ecotope.quartz.module.opentsdb.response.Response;
import com.ecotope.quartz.module.service.AccessDataService;
import com.ecotope.quartz.module.service.AtmosphericStationService;
import com.ecotope.quartz.module.service.TownAtmosphericStationService;
import com.ecotope.quartz.module.util.DateUtil;
import com.ecotope.quartz.module.util.HttpURLConnectionUtil;
import com.ecotope.quartz.module.util.WeekUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * ZD 空气质量站点 通过省平台接口获取数据并存入opentsdb
 */
@Slf4j
@Component
@EnableAsync
@EnableScheduling
@SuppressWarnings("all")
public class AtmosphereSchedule {

    //省平台地址
    private String baseUrl = "http://59.202.42.88:8180/sjmh/dynamicservice/dynamicquery";

    //大气乡镇
    private final static String getDataQTownByxzqh = "http://10.33.70.133:6081/rest/api/foreign/getDataQTown";

    //页大小
    private Integer pageSize = 10000;

    //页码
    private Integer pageNum = 1;

    //ZD
    private String fwxhZd = "075bb3abdb0046dd9b539497b55c2511";
    private String keyZd = "ff6e7e43-a879-4ef5-ae58-007c6cbe4388";

    //CS
    private String fwxhCs = "6b16a24fe0344a09a27db2b85a942826";
    private String keyCs = "3b830b37-0327-49d4-8337-324cf03e6eee";

    //QX
    private String fwxhQx = "f9fa13d4b3f2411fac31cb0c38de39a9";
    private String keyQx = "812e655c-2d94-4a6f-af24-5074868eb502";

    //PJ
    private String fwxhPj = "1fb0c439409e4d5e9e20a9be146c6a1b";
    private String keyPj = "941d260b-103f-4aef-b3d8-3718ae46fad9";

    @Autowired
    private AtmosphericStationService atmosphericStationService;

    @Autowired
    private AccessDataService accessDataService;

    @Autowired
    private TownAtmosphericStationService townAtmosphericStationService;

    /**
     * ZD 空气站点 （每小时执行）
     */
    @Scheduled(cron = "0 20 * * * ?")
    @Async
    public void getZDDateAndInsertOpentsdb() throws Throwable {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");
        Date date = new Date();
        String JSSJ = df.format(date);

        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH");
        String KSSJ = dfs.format(date);
        Date dateNew = date;
        Calendar calendarNew = Calendar.getInstance();
        calendarNew.setTime(dateNew);
        calendarNew.add(Calendar.DATE, -1);
        dateNew = calendarNew.getTime();
        KSSJ = df.format(dateNew);

        String s = WeekUtil.dayForWeek(JSSJ);
        if (s.equals("1")) {
            if (JSSJ.contains("00")) {
                calendarNew.add(Calendar.DATE, -7);
                dateNew = calendarNew.getTime();
                KSSJ = df.format(dateNew);
            }
        }
        HttpClientImpl client = new HttpClientImpl("http://61.164.218.155:61242");
        try {
            String kssj = URLEncoder.encode(KSSJ, "utf-8");
            String jssj = URLEncoder.encode(JSSJ, "utf-8");
            String url = baseUrl + "?fwxh=" + fwxhZd + "&key=" + keyZd + "&pagenum=" + pageNum + "&pageSize=" + pageSize + "&KSSJ=" + kssj + "&JSSJ=" + jssj;
            String methodReturn = HttpURLConnectionUtil.getMethod(url);
            if (HttpURLConnectionUtil.isJsonObject(methodReturn)) {
                JSONObject jsonObject = JSONObject.parseObject(methodReturn);
                JSONObject data = jsonObject.getJSONObject("data");
                String kqzlzdxssjDay = data.getString("KQZLZDXSSJ");

                if (kqzlzdxssjDay != null && !kqzlzdxssjDay.equals("[]")) {
                    //mysql数据库 记录数据插入时间
                    AccessData accessData = AccessData.builder().type(1).url("小时数据===>" + KSSJ + "--" + JSSJ).data("空气站点").build();
                    accessDataService.insert(accessData);
                    log.info("insert ZD-data-opentsdb start:");
                    //插入opentsdb
                    JSONArray jsonArray = JSONArray.parseArray(kqzlzdxssjDay);
                    for (Object o : jsonArray) {
                        Map map = JSONObject.parseObject(o.toString(), Map.class);
                        if (map.size() > 0) {
                            //移除不必要的数据
                            map.remove("CJSJ");
                            map.remove("ZDMC");
                            map.remove("XGSJ");
                            map.remove("XH");
                            //空气质量指数级别
                            DateUtil.KQZLZSJBMap(map);
                            //空气质量颜色
                            DateUtil.KQZLYSMap(map);
                            //空气质量类别
                            DateUtil.KQZLLBMap(map);
                            //首要污染物
                            DateUtil.SYWRWMap(map);
                            //转换时间戳
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            if (map.get("JCSJ") != null) {
                                long timestamp = sdf.parse(String.valueOf(map.get("JCSJ"))).getTime();
                                map.remove("JCSJ");
                                //根据站点编号查询接入编码
                                String zdbh = String.valueOf(map.get("ZDBH"));
                                map.remove("ZDBH");
                                AtmosphericStation atmosphericStation = AtmosphericStation.builder().uuid(zdbh).build();
                                List<AtmosphericStation> atmosphericStations = atmosphericStationService.selectList(atmosphericStation);
                                String host = null;
                                if (atmosphericStations.size() > 0) {
                                    host = atmosphericStations.get(0).getJrbm();
                                } else {
                                    host = zdbh;
                                }
                                MetricBuilder builder = MetricBuilder.getInstance();
                                for (Object entry : map.entrySet()) {
                                    Map.Entry<String, Object> map1 = (Map.Entry<String, Object>) entry;
                                    Double value = null;
                                    if (String.valueOf(map1.getValue()).contains(".") && String.valueOf(map1.getValue()).substring(0, String.valueOf(map1.getValue()).lastIndexOf(".")).contains(".")) {
                                        value = Double.parseDouble(String.valueOf(map1.getValue()).substring(0, String.valueOf(map1.getValue()).lastIndexOf(".")));
                                    } else {
                                        value = Double.parseDouble(String.valueOf(map1.getValue()));
                                    }
                                    builder.addMetric("mete.air").setDataPoint(timestamp, value).addTag("host", host).addTag("index", map1.getKey().toLowerCase());
                                }
                                //存入opentsdb
                                Response response = client.pushMetrics(builder,
                                        ExpectResponse.SUMMARY);
                                if (!response.isSuccess()) {
                                    log.info("insert opentsdb data fail!map is null!");
                                }
                            }
                        }
                    }
                    log.info("insert ZD-data-opentsdb end");
                } else {
                    log.info("返回数据为空");
                    AccessData accessData = AccessData.builder().type(1).url("返回数据为空" + KSSJ + "--" + JSSJ).data("空气站点").build();
                    accessDataService.insert(accessData);
                }
            } else {
                log.info(methodReturn);
                AccessData accessData = AccessData.builder().type(1).url(methodReturn + KSSJ + "--" + JSSJ).data("空气站点").build();
                accessDataService.insert(accessData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * CS 空气城市 （每小时 执行）
     */
    @Scheduled(cron = "0 25 * * * ?")
    @Async
    public void getCSDateAndInsertOpentsdb() throws Throwable {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");
        Date date = new Date();
        String JSSJ = df.format(date);

        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH");
        String KSSJ = dfs.format(date);
        Date dateNew = date;
        Calendar calendarNew = Calendar.getInstance();
        calendarNew.setTime(dateNew);
        calendarNew.add(Calendar.DATE, -1);
        dateNew = calendarNew.getTime();
        KSSJ = df.format(dateNew);

        String s = WeekUtil.dayForWeek(JSSJ);
        if (s.equals("1")) {
            if (JSSJ.contains("00")) {
                calendarNew.add(Calendar.DATE, -7);
                dateNew = calendarNew.getTime();
                KSSJ = df.format(dateNew);
            }
        }

        HttpClientImpl client = new HttpClientImpl("http://61.164.218.155:61242");
        try {
            //空气质量城市请求地址——省平台
            String kssj = URLEncoder.encode(KSSJ, "utf-8");
            String jssj = URLEncoder.encode(JSSJ, "utf-8");
            String url = baseUrl + "?fwxh=" + fwxhCs + "&key=" + keyCs + "&pagenum=" + pageNum + "&pageSize=" + pageSize + "&KSSJ=" + kssj + "&JSSJ=" + jssj;
            String methodReturn = HttpURLConnectionUtil.getMethod(url);
            if (HttpURLConnectionUtil.isJsonObject(methodReturn)) {
                JSONObject jsonObject = JSONObject.parseObject(methodReturn);
                JSONObject data = jsonObject.getJSONObject("data");
                String kqzlcsxssjDay = data.getString("KQZLCSXSSJ");
                //mysql数据库 记录数据插入时间
                if (kqzlcsxssjDay != null && !kqzlcsxssjDay.equals("[]")) {
                    AccessData accessData = AccessData.builder().type(2).url("小时数据===>" + KSSJ + "--" + JSSJ).data("空气城市").build();
                    accessDataService.insert(accessData);
                    log.info("insert cs-opentsdb data start:");
                    //插入opentsdb
                    JSONArray jsonArray = JSONArray.parseArray(kqzlcsxssjDay);
                    for (Object o : jsonArray) {
                        Map map = JSONObject.parseObject(o.toString(), Map.class);

                        if (map.size() > 0) {
                            //移除不必要的数据
                            map.remove("CJSJ");
                            map.remove("ZDMC");
                            map.remove("XGSJ");
                            map.remove("XH");
                            map.remove("XZQHMC");

                            //空气质量指数级别
                            DateUtil.KQZLZSJBMap(map);
                            //空气质量颜色
                            DateUtil.KQZLYSMap(map);
                            //空气质量类别
                            DateUtil.KQZLLBMap(map);
                            //首要污染物
                            DateUtil.SYWRWMap(map);
                            //行政区

                            //转换时间戳
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            if (map.get("JCSJ") != null) {
                                long timestamp = sdf.parse(String.valueOf(map.get("JCSJ"))).getTime();
                                map.remove("JCSJ");
                                String host = String.valueOf(map.get("XZQHDM"));
                                map.remove("XZQHDM");
                                MetricBuilder builder = MetricBuilder.getInstance();
                                for (Object entry : map.entrySet()) {
                                    Map.Entry<String, Object> map1 = (Map.Entry<String, Object>) entry;
                                    Double value = null;
                                    if (String.valueOf(map1.getValue()).contains(".") && String.valueOf(map1.getValue()).substring(0, String.valueOf(map1.getValue()).lastIndexOf(".")).contains(".")) {
                                        value = Double.parseDouble(String.valueOf(map1.getValue()).substring(0, String.valueOf(map1.getValue()).lastIndexOf(".")));
                                    } else {
                                        value = Double.parseDouble(String.valueOf(map1.getValue()));
                                    }
                                    builder.addMetric("mete.air").setDataPoint(timestamp, value).addTag("host", host).addTag("index", map1.getKey().toLowerCase());
                                }
                                //存入opentsdb
                                Response response = client.pushMetrics(builder,
                                        ExpectResponse.SUMMARY);
                                if (!response.isSuccess()) {
                                    log.info("insert cs-opentsdb data fail! map is null!");
                                }
                            }
                        }
                    }
                    log.info("insert cs-opentsdb data end");
                } else {
                    AccessData accessData = AccessData.builder().type(2).url("返回数据为空" + KSSJ + "--" + JSSJ).data("空气城市").build();
                    accessDataService.insert(accessData);
                    log.info("返回数据为空");
                }
            } else {
                log.info(methodReturn);
                AccessData accessData = AccessData.builder().type(2).url(methodReturn + KSSJ + "--" + JSSJ).data("空气城市").build();
                accessDataService.insert(accessData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * QX 清新空气
     */
    @Scheduled(cron = "0 45 * * * ?")
    @Async
    public void getQXDateAndInsertOpentsdb() {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");
        Date date = new Date();
        String JSSJ = df.format(date);

        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH");
        String KSSJ = dfs.format(date);
        Date dateNew = date;
        Calendar calendarNew = Calendar.getInstance();
        calendarNew.setTime(dateNew);
        calendarNew.add(Calendar.DATE, -1);
        dateNew = calendarNew.getTime();
        KSSJ = df.format(dateNew);

        HttpClientImpl client = new HttpClientImpl("http://61.164.218.155:61242");
        //清新空气站点请求地址——省平台
        try {
            String kssj = URLEncoder.encode(KSSJ, "utf-8");
            String jssj = URLEncoder.encode(JSSJ, "utf-8");
            String url = baseUrl + "?fwxh=" + fwxhQx + "&key=" + keyQx + "&pagenum=" + pageNum + "&pageSize=" + pageSize + "&KSSJ=" + kssj + "&JSSJ=" + jssj;
            String methodReturn = HttpURLConnectionUtil.getMethod(url);
            if (HttpURLConnectionUtil.isJsonObject(methodReturn)) {
                JSONObject jsonObject = JSONObject.parseObject(methodReturn);
                JSONObject data = jsonObject.getJSONObject("data");
                String qxkqzdxssjDay = data.getString("QXKQZDXSSJ");

                if (qxkqzdxssjDay != null && !qxkqzdxssjDay.equals("[]")) {
                    log.info("insert qx-opentsdb data start:");
                    //mysql数据库 记录数据插入时间
                    AccessData accessData = AccessData.builder().type(3).url("小时数据===>" + KSSJ + "--" + JSSJ).data("清新空气").build();
                    accessDataService.insert(accessData);
                    //插入opentsdb
                    JSONArray jsonArray = JSONArray.parseArray(qxkqzdxssjDay);
                    for (Object o : jsonArray) {
                        Map map = JSONObject.parseObject(o.toString(), Map.class);
                        if (map.size() > 0) {
                            //移除不必要的数据
                            map.remove("CJSJ");
                            map.remove("XGSJ");
                            map.remove("XH");
                            map.remove("PXH");
                            map.remove("BZ");
                            //数据标记
                            DateUtil.SJBJMap(map);

                            String yzbh = String.valueOf(map.get("YZBH"));
                            map.remove("YZBH");
                            String sjbj = String.valueOf(map.get("SJBJ"));
                            map.remove("SJBJ");
                            //转换时间戳
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            if (map.get("JCSJ") != null) {
                                long timestamp = sdf.parse(String.valueOf(map.get("JCSJ"))).getTime();
                                String host = String.valueOf(map.get("ZDBH"));
                                MetricBuilder builder = MetricBuilder.getInstance();
                                map.remove("JCSJ");
                                map.remove("ZDBH");
                                for (Object entry : map.entrySet()) {
                                    Map.Entry<String, Object> map1 = (Map.Entry<String, Object>) entry;
                                    Double value = null;
                                    if ("--".equals(map1.getValue()) || StringUtils.isEmpty((String) map1.getValue())) {
                                        value = 0.0;
                                    } else {
                                        value = Double.parseDouble(String.valueOf(map1.getValue()));
                                    }
                                    builder.addMetric("mete.air").setDataPoint(timestamp, value).addTag("yzbh", yzbh)
                                            .addTag("sjbj", sjbj).addTag("host", host).addTag("index", map1.getKey().toLowerCase());
                                }
                                //存入opentsdb
                                Response response = client.pushMetrics(builder,
                                        ExpectResponse.SUMMARY);
                                if (!response.isSuccess()) {
                                    log.info("insertopentsdb data fail! map is null!");
                                }
                            }
                        }
                    }
                    log.info("insert qx-opentsdb data end");
                } else {
                    log.info("返回数据为空");
                    AccessData accessData = AccessData.builder().type(3).url("返回数据为空" + KSSJ + "--" + JSSJ).data("清新空气").build();
                    accessDataService.insert(accessData);
                }
            } else {
                log.info(methodReturn);
                AccessData accessData = AccessData.builder().type(3).url(methodReturn + KSSJ + "--" + JSSJ).data("清新空气").build();
                accessDataService.insert(accessData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * PJ 空气评价（每小时 执行）
     */
    @Scheduled(cron = "0 50 * * * ?")
    @Async
    public void getPJDateAndInsertOpentsdb() throws Throwable {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");
        Date date = new Date();
        String JSSJ = df.format(date);

        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH");
        String KSSJ = dfs.format(date);
        Date dateNew = date;
        Calendar calendarNew = Calendar.getInstance();
        calendarNew.setTime(dateNew);
        calendarNew.add(Calendar.DATE, -1);
        dateNew = calendarNew.getTime();
        KSSJ = df.format(dateNew);

        String s = WeekUtil.dayForWeek(JSSJ);
        if (s.equals("1")) {
            if (JSSJ.contains("00")) {
                calendarNew.add(Calendar.DATE, -7);
                dateNew = calendarNew.getTime();
                KSSJ = df.format(dateNew);
            }
        }

        HttpClientImpl client = new HttpClientImpl("http://61.164.218.155:61242");
        //空气质量评价请求地址——省平台
        try {
            String kssj = URLEncoder.encode(KSSJ, "utf-8");
            String jssj = URLEncoder.encode(JSSJ, "utf-8");
            String url = baseUrl + "?fwxh=" + fwxhPj + "&key=" + keyPj + "&pagenum=" + pageNum + "&pageSize=" + pageSize + "&KSSJ=" + kssj + "&JSSJ=" + jssj;

            String methodReturn = HttpURLConnectionUtil.getMethod(url);
            if (HttpURLConnectionUtil.isJsonObject(methodReturn)) {
                JSONObject jsonObject = JSONObject.parseObject(methodReturn);
                JSONObject data = jsonObject.getJSONObject("data");
                String qxkqzdxssjpjDay = data.getString("QXKQZDXSSJPJ");
                if (qxkqzdxssjpjDay != null && !qxkqzdxssjpjDay.equals("[]")) {
                    //mysql数据库 记录数据插入时间
                    AccessData accessData = AccessData.builder().type(4).url("小时数据===>" + KSSJ + "--" + JSSJ).data("空气评价").build();
                    accessDataService.insert(accessData);
                    log.info("insert pj-opentsdb data start");
                    //插入opentsdb
                    JSONArray jsonArray = JSONArray.parseArray(qxkqzdxssjpjDay);
                    for (Object o : jsonArray) {
                        Map map = JSONObject.parseObject(o.toString(), Map.class);

                        if (map.size() > 0) {
                            //移除不必要的数据
                            map.remove("CJSJ");
                            map.remove("XGSJ");
                            map.remove("XH");
                            map.remove("BZ");

                            //评价等级
                            DateUtil.PJDJMap(map);

                            //转换时间戳
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            if (map.get("PJSJ") != null) {
                                long timestamp = sdf.parse(String.valueOf(map.get("PJSJ"))).getTime();
                                String host = String.valueOf(map.get("ZDBH"));
                                MetricBuilder builder = MetricBuilder.getInstance();
                                map.remove("PJSJ");
                                map.remove("ZDBH");
                                for (Object entry : map.entrySet()) {
                                    Map.Entry<String, Object> map1 = (Map.Entry<String, Object>) entry;
                                    Double value = null;
                                    if ("--".equals(map1.getValue())) {
                                        value = 0.0;
                                    } else {
                                        value = Double.parseDouble(String.valueOf(map1.getValue()));
                                    }
                                    builder.addMetric("mete.air").setDataPoint(timestamp, value).addTag("host", host).addTag("index", map1.getKey().toLowerCase());
                                }
                                //存入opentsdb
                                Response response = client.pushMetrics(builder,
                                        ExpectResponse.SUMMARY);
                                if (!response.isSuccess()) {
                                    log.info("insert pj_opentsdb data fail! map is null!");
                                }
                            }
                        }
                    }
                    log.info("insert pj-opentsdb data end!");
                } else {
                    log.info("返回数据为空");
                    //mysql数据库 记录数据插入时间
                    AccessData accessData = AccessData.builder().type(4).url("返回数据为空" + KSSJ + "--" + JSSJ).data("空气评价").build();
                    accessDataService.insert(accessData);
                }
            } else {
                log.info(methodReturn);
                AccessData accessData = AccessData.builder().type(4).url(methodReturn + KSSJ + "--" + JSSJ).data("空气评价").build();
                accessDataService.insert(accessData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * Town 大气乡镇 (每 小时 执行)
     */
    @Scheduled(cron = "0 31 * * * ?")
    @Async
    public void getTownDataInsertOpentsdb() throws Throwable {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");
        Date date = new Date();
        String JSSJ = df.format(date);

        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH");
        String KSSJ = dfs.format(date);
        Date dateNew = date;
        Calendar calendarNew = Calendar.getInstance();
        calendarNew.setTime(dateNew);
        calendarNew.add(Calendar.DATE, -1);
        dateNew = calendarNew.getTime();
        KSSJ = df.format(dateNew);

        String s = WeekUtil.dayForWeek(JSSJ);
        if (s.equals("1")) {
            if (JSSJ.contains("00")) {
                calendarNew.add(Calendar.DATE, -7);
                dateNew = calendarNew.getTime();
                KSSJ = df.format(dateNew);
            }
        }

        HttpClientImpl client = new HttpClientImpl("http://61.164.218.155:61242");

            try {
                String type = URLEncoder.encode("时均值", "utf-8");
                String startTime = URLEncoder.encode(KSSJ, "utf-8");
                String endTime = URLEncoder.encode(JSSJ, "utf-8");
                String url = getDataQTownByxzqh + "&type=" + type + "&startTime=" + startTime + "&endTime=" + endTime;
                String methodReturn = HttpURLConnectionUtil.getMethod(url);
                if (null != methodReturn && !methodReturn.equals("[]")) {
                    //mysql数据库 记录数据插入时间
                    AccessData accessData = AccessData.builder().type(6).url("小时数据===>" + startTime + "--" + endTime).data("大气乡镇").build();
                    accessDataService.insert(accessData);
                    JSONArray jsonArray = JSONArray.parseArray(methodReturn);
                    for (Object o : jsonArray) {
                        Map map = JSONObject.parseObject(o.toString(), Map.class);
                        if (map.size() > 0) {
                            //转换时间戳
                            long timestamp = (long) map.get("JCSJ");
                            //根据站点名称获取设备ID
                            String zdmc = (String) map.get("ZDMC");
                            String host = townAtmosphericStationService.selectZDBH(zdmc);
                                map.remove("JCSJ");
                                map.remove("ZDMC");
                                map.remove("ID");
                                map.remove("GMT_MODIFIED");
                                map.remove("GMT_CREATE");
                                MetricBuilder builder = MetricBuilder.getInstance();
                                for (Object entry : map.entrySet()) {
                                    Map.Entry<String, String> map1 = (Map.Entry<String, String>) entry;
                                    Double value = Double.parseDouble(map1.getValue());
                                    builder.addMetric("mete.atmospheric").setDataPoint(timestamp, value).addTag("host", host)
                                            .addTag("index", map1.getKey().toLowerCase());
                                }
                                //存入opentsdb
                                Response response = client.pushMetrics(builder,
                                        ExpectResponse.SUMMARY);
                                if (!response.isSuccess()) {
                                    log.info("town data insert fail---map is null！");
                                }
                        }
                    }
                }else {
                    AccessData accessData = AccessData.builder().type(6).url("小时数据为空===>" + startTime + "--" + endTime).data("大气乡镇").build();
                    accessDataService.insert(accessData);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

}
